using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using PHSRAG.Utility;
using PHSRAG.Insight21.Admin;

namespace PHSRAG.Insight21.Admin.Controls
{
	/// <summary>
	///	Allows assignment of proper modules to users being given access to a lab.
	/// </summary>
	public partial class EditAdminLabAccessModules : System.Web.UI.UserControl
	{
		#region Constants
		/// <summary>Key used to check if the control is loaded for the first time or as a result of a client postback.</summary>
		private const string ControlPostBackKey = "ControlPostBackKey_C30C21C5-9BD6-4d5a-8BF6-02EB9019F262";
		#endregion

		#region Private Variables
		/// <summary>Reference to the containing page (.aspx). </summary>
		private EditAdminLabAccessWizard			hostPage;
		/// <summary>Policy used for caching business objects.</summary>
		private	Policy.ICachePolicy		objectCachePolicy;
		#endregion

		#region Private Properties
		/// <summary>
		/// Reinforces the notion of "IsPostBack" within this user control as UserControl.IsPostBack
		/// is the same as Page.IsPostBack which will not work in this case because we have one page and
		/// multiple controls.
		/// </summary>
		private bool IsUserControlPostBack 
		{
			get { return ViewState[ControlPostBackKey] != null; }
			set { ViewState.Add(ControlPostBackKey, value); }
		}
		#endregion

		#region Event Handlers
		/// <summary>
		/// Initializes the host page and loads the datagrid with
		/// lab access modules.
		/// </summary>
		/// <param name="sender">The object raising an event.</param>
		/// <param name="e">Argument associated with the event</param>
		protected void Page_Load(object sender, System.EventArgs e)
		{
			hostPage = (EditAdminLabAccessWizard)Page;
			if (!IsUserControlPostBack)
			{
				IsUserControlPostBack = true;
				LoadDataGrid();
			}
		}

		/// <summary>
		/// Occurs when items are bound to a datagrid.
		/// </summary>
		/// <param name="sender">The object raising an event.</param>
		/// <param name="e">Argument associated with the event</param>
		protected void OnItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
			{
				DataRowView view =  (DataRowView)e.Item.DataItem;
				if( hostPage.CurrentLab.Access.Rows[0].RowState != System.Data.DataRowState.Deleted)
				{
					int accessRightTypeBitmask = (int)view["AccessRightTypeBitmask"];
					object rights = hostPage.CurrentLab.Access.Rows[0]["RightBitMask"];
					if (rights != DBNull.Value)
						((CheckBox)e.Item.FindControl("isSelected")).Checked =
							(accessRightTypeBitmask & (int)rights) != 0;
				}
			}
		}

		/// <summary>
		/// Validates that at least one module is selected.
		/// </summary>
		/// <param name="source">The object raising an event.</param>
		/// <param name="args">Argument associated with the event</param>
		protected void OnServerValidate(object source, ServerValidateEventArgs args)
		{
			bool noneSelected = false;
			foreach (DataGridItem item in dataGrid.Items) 
				if (!noneSelected)
					noneSelected = ((CheckBox)item.FindControl("isSelected")).Checked;
				else
					break;
			args.IsValid = noneSelected;
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Loads the datagrid with insight modules information.
		/// </summary>
		private void LoadDataGrid()
		{
			Modules labModuleAccess = new Modules(objectCachePolicy);
			labModuleAccess.Load(DataAccess.GetConnectionString(), ModulesCommandType.GetLabAccessModules);
			dataGrid.DataSource = labModuleAccess.Details;
			dataGrid.DataBind();
		}
		#endregion

		#region Web Form Designer generated code
		/// <summary>
		/// Wires up page events.
		/// </summary>
		/// <param name="e">Information associated with the event.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			ID = "editAdminLabAccessModules";

			objectCachePolicy = (Policy.ICachePolicy)Session[Policy.Names.CachePolicyKey];
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}
		#endregion
	}
}
